
<template>
  <div class="yy-announcement-management">
    <!--列表-->
    <div class="yy-announcement-management-list" v-show="type=='list'">
      <Card>
        <p slot="title">
          <Icon type="md-list" />
          公告管理
        </p>
        <Form ref="form" :model="formSearch"  inline>
            <FormItem prop="user">
              <Input type="text" v-model="formSearch.msgTitle" placeholder="公告标题"></Input>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleSearch()">查询</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAdd()">新增</Button>
            </FormItem>
        </Form>
        <Table :columns="columns" :data="data" ></Table>
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
          </div>
        </div>
      </Card>
    </div>
    <!--详情-->
    <div class="yy-announcement-management-details" v-show="type=='details'">
       <Card>
        <p slot="title">
          <Icon type="md-paper"/>
          查看详情
        </p>
        <p slot="extra">
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="details" :data="detailsList">
          <p class="details-title"><span>标题:</span>{{currentData.msgTitle}}</p>
          <p class="details-title"><span>公告内容:</span></p>
          <Card  class="contract-upload-list-content">{{currentData.msgContent}}</Card>
          <p class="details-title"><span>发布时间:</span>{{currentData.msgDate}}</p>
          <input class="details-title" type="radio" name="radios" value="立即发布" v-model="param" style="vertical-align:top"><label>立即发布</label>
          <input class="details-title" type="radio" name="radios" value="存为草稿" v-model="param" style="vertical-align:top"><label>存为草稿</label>
        </div>
      </Card>
    </div>
    <!--添加/编辑-->
    <div class="yy-announcement-management-add-edit"  v-show="(type=='add' || type=='edit')">
      <Card>
        <p slot="title">
          <template v-if="type=='add'"> 
            <Icon type="md-create"/> 新增
          </template>
          <template v-if="type=='edit'"> 
            <Icon type="md-document"/>编辑
          </template>
        </p>
        <p slot="extra" >
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="editForm">
          <Form ref="editForm" :model="editFormInLine" :rules="editFormRule" :label-width="100" @keydown.native.enter.prevent ="handleSubmit">
             <FormItem label="标题" prop="msgTitle">
              <Input v-model="editFormInLine.msgTitle" placeholder="请输入标题"></Input>
            </FormItem>
            <FormItem label="公告内容" prop="msgContent">
              <Input v-model="editFormInLine.msgContent" type="textarea" :autosize="{minRows: 8,maxRows: 10}" placeholder="请输入公告内容" maxlength="1000" ></Input>
            </FormItem>
            <FormItem label="发布时间" prop="msgDate">
            <DatePicker v-model="editFormInLine.msgDate" placeholder="请选择发布时间" :options="endTimeOption" @on-change="onEndTimeChange" type="date"></DatePicker>
          </FormItem>
          <FormItem>
           <input class="details-title" type="radio" name="radios" value="立即发布" v-model="param" style="vertical-align:top"><label>立即发布</label>
           <input class="details-title" type="radio" name="radios" value="存为草稿" v-model="param" style="vertical-align:top"><label>存为草稿</label>
          </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" class="button">确定</Button>
              <Button @click="handleReset" type="primary" class="button">重置</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatNetTime } from "@/libs/tools";
import i18n from "./locale";

export default {
  name: "yy-announcement-management",
  data() {
    return {
      i18n: i18n,
      currentData: "",
      detailsModal: false,
      endTimeOption: {
        disabledDate(date) {
          return date && date.valueOf() < Date.now() - 86400000;
        }
      },
      type: "list",
      page: {
        total: 0,
        pageSize: 20
      },
      formSearch: {
        msgTitle: ""
      },
      editFormInLine: {
        msgId: "",
        msgTitle: "",
        msgContent: "",
        msgDate: "",
        msgType: "",
        msgHidden: "",
        userName: "chenweihan",
        UserID: "chenweihan"
      },
      editFormRule: {
        msgTitle: ValidatorRule.require
      },
      columns: [
        {
          title: "公告标题",
          key: "msgTitle"
        },
        {
          title: "发布日期 ",
          key: "msgDate"
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.details(params.index);
                    }
                  }
                },
                "查看"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.index);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.delete(params.index, params.row.id);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: [],
      detailsList: []
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.handleSearch();
  },

  computed: {},

  methods: {
    handleSearch() {
      let obj = this.formSearch;
      obj.pageNum = 1;
      obj.pageSize = this.page.pageSize;
      this.formSearch = obj;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            if (data != null) {
              this.page.total = data.total;
              this.page.pageSize = data.pageSize;
              this.data = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.data = [];
            }
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    changePage(pageNum) {
      let obj = this.formSearch,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = obj;
      this.getList(obj);
    },

    goBack() {
      this.type = "list";
      //console.log(276, this.data);
    },

    handleAdd() {
      this.type = "add";
      this.handleReset();
    },

    details(index) {
      this.type = "details";
      this.currentData = this.data[index];
      let obj = {
        msgId: ""
      };
      API.details(obj)
        .then(res => {
          if (res.code == 200) {
            let dataDet = res.data;
            this.detailsList = dataDet.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    edit(index) {
      this.handleReset();
      this.type = "edit";
      this.currentData = this.data[index];
      this.editFormInLine = _.clone(this.currentData);
      this.editFormInLine.msgDate = new Date();
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            UserID: tthis.userInfo.userId,
            msgId: ""
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              console.log(err);
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = this.formSearch;
      this.getList(obj);
    },

    handleSubmit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    handleReset() {
      this.$refs.editForm.resetFields();
    },

    editSubmit(obj) {
      obj.id = this.currentData.id;
      API.editSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.resetList();
            this.$Message.success("修改成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSearch();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./yy-announcement-management.less";
</style>
